package nu.aing.P20140609;

import nu.aing.utils.TestUtils;

/**
 * @author memekmaut@gmail.com
 */
public class FibonacciDiv2 {
    
    public static void main(String[] args) {
        FibonacciDiv2 obj = new FibonacciDiv2();
        TestUtils.test(obj, "find", 1);
        TestUtils.test(obj, "find", 13);
        TestUtils.test(obj, "find", 15);
        TestUtils.test(obj, "find", 19);
        TestUtils.test(obj, "find", 1000000);
    }
    
    public int find(int N) {
        int x = 0, y = 1, z = -1;
        while(true) {
            if(x >= N || y >= N) {
                x = N - x < 0 ? -(N - x) : (N - x);
                y = y - N < 0 ? -(y - N) : (y - N);
                z = N - z < 0 ? -(N - z) : (N - z);
                return x > y ? (y > z ? z : y) : x > z ? z : x;
            }
            y = up((x = up(x, y)), (z = y));
        }
    }
    
    private int up(int a, int b) {
        return a + b;
    }
    
}
